<?php
namespace app\admin\controller;
use app\admin\controller\Permissions;
use app\admin\model\Kxffhz as kxffhzModel;
use think\Config;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
class Kxffhz extends Permissions
{

    public function index()
    {
        $list = db('population')->where(['isdel'=>1])->select();
        $moneyArr = [];
        foreach ($list as $k => &$v) {
            $money = db('kxffhz')->where(['pid'=>$v['id']])->sum('money');
            $v['ffmoney']= $money;
            $moneyArr[] = $money;
        }
        if($moneyArr) $allMoney = array_sum($moneyArr); else $allMoney = 0;
        $this->assign('allMoney',$allMoney);
        $this->assign('list',$list);
        return $this->fetch();
    }


    public function delete()
    {
        if (request()->isAjax()) {
            $id = input('id');
            $model = new kxffhzModel();
            $isok = $model->where('id', $id)->delete();
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
                $arr['msg'] = "删除成功！";
            } else {
                $arr['code'] = 2;
                $arr['msg'] = "删除失败！";
            }
            return $arr;
        }
    }


    //年龄
    function ageVerification($code){
        $age_time = strtotime(substr($code, 6, 8));
        if($age_time === false){
            return false;
        }
        list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age_time));

        $now_time = strtotime("now");

        list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now_time));
        $age = $y2 - $y1;
        if((int)($m2.$d2) < (int)($m1.$d1)){
            $age -= 1;
        }
        return $age;
    }

    public function info()
    {
        $pid = input('id');
        $zhu = db('population')->where(['id'=>$pid])->find();

        $list = db('kxffhz')->where(['pid'=>$pid])->select();


        $allMoney = db('kxffhz')->where(['pid'=>$pid])->sum('money');

        if (request()->isAjax()) {
            $post = input('post.');
            $res = db('kxffhz')->insert($post);
            if ($res) {
                return 200;
            }else{
                return 202;
            }
            return $arr;
        }
        $this->assign('zhu',$zhu);
        $this->assign('allMoney',$allMoney);
        $this->assign('list',$list);

        $admin_cate_id = Session::get('admin_cate_id');
        $this->assign('admin_cate_id',$admin_cate_id);
        $operation = Config::get('operation');
        $this->assign('operation',$operation);

        return $this->fetch();
    }
    

    public function edit(){
        $input = input('get.');

        if (request()->isAjax()) {
            $input = input('post.');
            $id = $input['id'];
            unset($input['id']);
            $isok = db('kxffhz')->where(['id' =>$id])->update($input);
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
            } else {
                $arr['code'] = 2;
                $arr['msg'] = '操作失败,请刷新重试';
            }
            return $arr;
        }
        $data = db('kxffhz')->where(['id' => $input['id']])->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
}
